define(function(require, exports) {
	var util = require('util');
	var pubjs = require('pubjs');
	var view  = require('@base/view');
	var dspUtil = require('dspUtil');

	// 平均温度checkbox候选项
	var LEVELS_ITEMS = [
		 {value: "0", label: LANG("-5℃以下")}
		,{value: "1", label: LANG("-5℃ ~ 5℃")}
		,{value: "2", label: LANG("5℃ ~ 15℃")}
		,{value: "3", label: LANG("15℃ ~ 25℃")}
		,{value: "4", label: LANG("25℃ ~ 35℃")}
		,{value: "5", label: LANG("35℃以上")}
	];

	// 编辑活动温度组件
	var Base = view.container.extend({
		init: function(config) {
			var self = this;
			config = pubjs.conf(config, {
				 'class': 'M-PageTemperature'
				,'tplFile': '/tpl/campaign/modules/temperature.tpl'
				,'view_model': {
					// 显示平均温度编辑框
					'_showLevels': false
					// 显示最低温度编辑框
					,'_showLow': false
					// 显示最高温度编辑框
					,'_showHigh': false
					// 平均温度checkbox候选项
					,'_levelsItems': util.extend({}, LEVELS_ITEMS)
					// 平均温度checkbox
					,'temperLevels': []
					// 平均温度天数
					,'dayLevels': '0'
					// 最低温度input
					,'temperLow': '0'
					// 最低温度天数
					,'dayLow': '0'
					// 最高温度input
					,'temperHigh': '0'
					// 最高温度天数
					,'dayHigh': '0'
					// 全选
					,evAll: self.eventAll.bind(self)
					// 反选
					,evInvert: self.eventInvert.bind(self)
				}
				,'tipOpts': {
					 'levelsTip': {
						width: 260
						,data: LANG("连续投放天数，填0表示当天投放。")
					}
					,'lowTip': {
						width: 260
						,data: LANG("连续投放天数，填0表示当天投放。")
					}
					,'highTip': {
						width: 260
						,data: LANG("连续投放天数，填0表示当天投放。")
					}
				}
			});

			self.Super('init', arguments);
		},
		afterBuild: function() {
			var self = this;
			var c = self.getConfig();
			var opts = {
				// 最低温度比较操作符
				 lowOperate: {
					'width': 120
					,'options': [
						{name: LANG('大于等于'), id: 1}
						,{name: LANG('小于等于'), id: 2}
					]
					,'value': 1
					,'panel_config': {
						'hasSearch': false
					}
				}
				// 最高温度比较操作符
				,highOperate: {
					'width': 120
					,'options': [
						{name: LANG('大于等于'), id: 1}
						,{name: LANG('小于等于'), id: 2}
					]
					,'value': 1
					,'panel_config': {
						'hasSearch': false
					}
				}
			};
			self.buildFromTemplate(util.extend(opts, c.tipOpts));
		}
		,reset: function() {
			this.vm.reset();
			return this;
		}
		,setData: function(data) {
			var self = this;
			var vm = self.vm.$;
			var levelsData = data["TemperatureLevels"];
			var lowData = data["LowTemperature"];
			var highData = data["HightTemperature"];

			if (levelsData) {
				vm.temperLevels = dspUtil.map(levelsData.result || [], String);
				vm.dayLevels = levelsData.openDays || '0';
				vm._showLevels = true;
			}

			if (lowData) {
				vm.temperLow = lowData.ChangeCount || '0';
				self.$.lowOperate.setValue(lowData.ChangeType);
				vm.dayLow = lowData.OpenDays || '0';
				vm._showLow = true;
			}

			if (highData) {
				vm.temperHigh = highData.ChangeCount || '0';
				self.$.highOperate.setValue(highData.ChangeType);
				vm.dayHigh = highData.OpenDays || '0';
				vm._showHigh = true;
			}
			return self;
		}
		,getData: function() {
			var data = {};
			var vm = this.vm.$;
			// 平均温度
			if(vm._showLevels){
				data["TemperatureLevels"] = {
					'filterType': 0,
					'result': vm.temperLevels,
					'openDays': vm.dayLevels
				}
			}
			if(vm._showLow){
				// 最低温度
				data["LowTemperature"] = {
					'ChangeCount': vm.temperLow,
					'ChangeType': this.$.lowOperate.getValue(true).id,
					'OpenDays': vm.dayLow
				}
			}
			if(vm._showHigh){
				// 最高温度
				data["HightTemperature"] = {
					'ChangeCount': vm.temperHigh,
					'ChangeType': this.$.highOperate.getValue(true).id,
					'OpenDays': vm.dayHigh
				}
			}

			return data;
		}
		,eventAll: function(ev) {
			ev.preventDefault();
			var alls = [];

			util.each(LEVELS_ITEMS, function(item) {
				alls.push(item.value);
			});
			this.vm.$.temperLevels = alls;
		}
		,eventInvert: function(ev) {
			ev.preventDefault();
			var origin = this.vm.$.temperLevels;
			var result = [];

			util.each(LEVELS_ITEMS, function(item) {
				if (!util.exist(origin, item.value)) {
					result.push(item.value);
				}
			});
			this.vm.$.temperLevels = result;
		}
	});

	exports.base = Base;
});